{
  "metadata" : {
    "name" : "Graph Plots",
    "user_save_timestamp" : "1970-01-01T01:00:00.000Z",
    "auto_save_timestamp" : "1970-01-01T01:00:00.000Z",
    "language_info" : {
      "name" : "scala",
      "file_extension" : "scala",
      "codemirror_mode" : "text/x-scala"
    },
    "trusted" : true,
    "customLocalRepo" : null,
    "customRepos" : null,
    "customDeps" : [ ],
    "customImports" : null,
    "customArgs" : null,
    "customSparkConf" : null
  },
  "cells" : [ {
    "metadata" : { },
    "cell_type" : "markdown",
    "source" : "#### Graph items can have any payload!"
  }, {
    "metadata" : {
      "trusted" : true,
      "input_collapsed" : false,
      "output_stream_collapsed" : true,
      "collapsed" : false
    },
    "cell_type" : "code",
    "source" : "case class NodePayload(name:String, value:Double)\ncase class EdgePayload(desc:String, value:Any)",
    "outputs" : [ ]
  }, {
    "metadata" : { },
    "cell_type" : "markdown",
    "source" : "#### We can plot a graph using `GraphChart` and the types `Node` and `Edge`."
  }, {
    "metadata" : {
      "trusted" : true,
      "input_collapsed" : false,
      "output_stream_collapsed" : true,
      "collapsed" : false
    },
    "cell_type" : "code",
    "source" : "val w = widgets.GraphChart(Seq(\n  Node(1, \"a\"), \n  Node(2, 10), \n  Edge(1, (1,2), (\"hello\", \"world\")),\n  Node(3, NodePayload(\"test\", 3.14), color=\"red\"),\n  Node(4, NodePayload(\"test2\", 2.718)),\n  Edge(2, (1, 3), \"a pi\"),\n  Edge(3, (2, 4), EdgePayload(\"blah blah\", Left(10e1)))\n), \nsizes=(300,150),\nmaxPoints=100)",
    "outputs" : [ ]
  }, {
    "metadata" : { },
    "cell_type" : "markdown",
    "source" : "Adding new nodes dynamically?"
  }, {
    "metadata" : {
      "trusted" : true,
      "input_collapsed" : false,
      "collapsed" : false
    },
    "cell_type" : "code",
    "source" : "w.addAndApply(Seq.tabulate(10)(i => Node(4+i, \"new-\"+i, color=\"blue\")))",
    "outputs" : [ ]
  }, {
    "metadata" : { },
    "cell_type" : "markdown",
    "source" : "Adding new edges dynamically?"
  }, {
    "metadata" : {
      "trusted" : true,
      "input_collapsed" : false,
      "collapsed" : false
    },
    "cell_type" : "code",
    "source" : "w.addAndApply(\n  Seq.tabulate(10){ i => \n    Edge(4+i, (scala.util.Random.nextInt(7), 7+scala.util.Random.nextInt(7)), \"new-edge-\"+i, color=\"red\")\n  }\n)",
    "outputs" : [ ]
  }, {
    "metadata" : { },
    "cell_type" : "markdown",
    "source" : "#### We will now generate a bunch of data"
  }, {
    "metadata" : {
      "trusted" : true,
      "input_collapsed" : false,
      "output_stream_collapsed" : true,
      "collapsed" : false
    },
    "cell_type" : "code",
    "source" : "val nodes = List.tabulate(100)(x => s\"node $x\")\n()",
    "outputs" : [ ]
  }, {
    "metadata" : {
      "trusted" : true,
      "input_collapsed" : false,
      "output_stream_collapsed" : true,
      "collapsed" : false
    },
    "cell_type" : "code",
    "source" : "val links = for {\n  (e1, i) <- nodes.zipWithIndex\n  (e2, j) <- nodes.zipWithIndex if scala.util.Random.nextDouble < 0.05 && e1 != e2\n} yield (i, j, s\"$i → $j\")\n()",
    "outputs" : [ ]
  }, {
    "metadata" : { },
    "cell_type" : "markdown",
    "source" : "Let's create Graphable data with random colors"
  }, {
    "metadata" : {
      "trusted" : true,
      "input_collapsed" : false,
      "output_stream_collapsed" : true,
      "collapsed" : false
    },
    "cell_type" : "code",
    "source" : "val colorAtoms = ('0' to 'F').toList ::: ('A' to 'F').toList\ndef color = \"#\"+scala.util.Random.shuffle(colorAtoms).take(3).mkString\nval gw = widgets.GraphChart(\n  nodes.zipWithIndex.map{case (v,i) => Node(i, v, color=color) } ::: \n    links.zipWithIndex.map { case ((i,j,v), k) => Edge(k, (i,j), v, color=color)},\n  maxPoints=nodes.size+links.size,\n  linkStrength=0.1\n)",
    "outputs" : [ ]
  } ],
  "nbformat" : 4
}